<template>
  <div class="boxs">
    <div class="app-container">
      <el-card>
        <!-- 标签页组件-->
        <el-tabs>
          <!-- 放置页签1 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="show=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination :total="total" :current-page="query.page" :page-size="query.pagesize" layout="prev,pager,next" @current-change="changePage" />
            </el-row>
          </el-tab-pane>
          <!-- 放置页签2 -->
          <el-tab-pane label="公司信息">
            页签内容
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="roleForm.id ? '新增角色' :'修改角色'" :visible.sync="show" @close="close">
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="show = false">取消</el-button>
            <el-button size="small" type="primary" @click="submit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addRoleApi, getRoleListApi, deleteRoleApi, updateRoleApi, getRoleDetailApi } from '@/api/setting'
export default {
  data() {
    return {
      // 新增角色表单数据
      roleForm: {
        name: '', // 新增角色名
        description: ''
      },
      // 表单校验规则
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      // 控制新增角色弹层显示
      show: false,
      // 角色列表
      list: [],
      // 数据总条数
      total: 0,
      // 获取角色列表查询条件
      query: {
        page: 1, // 页码
        pagesize: 2 // 每页条数
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 点击删除角色
    delRole(row) {
      this.$confirm(`确定删除:${row.name}吗？`).then(async() => {
        // 确定
        await deleteRoleApi(row.id)
        this.$message.success('删除成功')
        this.getRoleList()
      }).catch(() => {
        // 取消
      })
    },
    // 关闭弹层
    close() {
      // 重置表单
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '', // 新增角色名
        description: ''
      }
    },
    async editRole(id) {
      const res = await getRoleDetailApi(id)
      this.roleForm = res
      this.show = true
    },
    // 点击新增角色
    submit() {
      this.$refs.roleForm.validate(async(isOk) => {
        if (isOk) {
          // 校验通过
          if (this.roleForm.id) {
            // 修改
            await updateRoleApi(this.roleForm)
            this.$message.success('更新角色成功')
          } else {
            // 新增
            await addRoleApi(this.roleForm)
            this.$message.success('新增成功')
          }
          this.show = false
          this.getRoleList()
        }
      })
    },
    // 换页
    changePage(currPage) {
      console.log('当前页码', currPage)
      // 1. 修改查询参数的页码
      this.query.page = currPage
      // 2. 重新查询列表
      this.getRoleList()
    },
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleListApi(this.query)
      // console.log(rows)
      this.total = total
      this.list = rows
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
